package com.didi.gulimall.aop;

import com.didi.gulimall.api.WareApiService;
import com.didi.gulimall.common.constant.RabbitMqConstant;
import com.didi.gulimall.common.entity.LocalMqEntity;
import com.didi.gulimall.common.utils.R;
import com.didi.gulimall.config.RouteKeyConfig;
import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Date;
import java.util.Set;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @Author 10626
 * @Create 2021/6/3 20:20
 */
@Aspect
@Component
@Slf4j
public class MqLogAspect {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private ThreadPoolExecutor executor;

    @Autowired
    private WareApiService wareApiService;

    @Autowired
    private RouteKeyConfig routeKeyConfig;

    //定义切入点，使用了该注解的方法将被AOP切入
    @Pointcut("@annotation( com.didi.gulimall.aop.MqLog)")
    public void logPointCut() {
    }

    /**
     * @param joinPoint
     * @return
     */
    @Before("logPointCut()")
    public void logBefore(JoinPoint joinPoint) throws JsonProcessingException {

        log.info("目标方法：" + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
        log.info("传入参数：" + Arrays.toString(joinPoint.getArgs()));
        Object[] args = joinPoint.getArgs();
        LocalMqEntity localMqEntities = (LocalMqEntity) args[0];
        localMqEntities.setStatus(RabbitMqConstant.NOT_CONSUMED);
        localMqEntities.setCreatedTime(new Date());
        localMqEntities.setRetries(0);

        Set<String> memberRouteKet = routeKeyConfig.getMemberRouteKet();
        Set<String> orderRouteKet = routeKeyConfig.getOrderRouteKet();
        Set<String> wareRouteKet = routeKeyConfig.getWareRouteKet();
        if (wareRouteKet.contains(localMqEntities.getRouteKey())) {
//                设置其他服务同步mq
            R r = wareApiService.saveLocalmq(localMqEntities);
            if (r.getCode() != 0) {
                log.error("同步消息错误，错误原因:{}", r.getMsg());
            }

        }
//        if (memberRouteKet.contains(localMqEntities.getRouteKey())) {
//            CompletableFuture.runAsync(() -> {
////                设置其他服务同步mq
//                R r = wareApiService.saveLocalmq(localMqEntities);
//                if (r.getCode() != 0) {
//                    log.error("同步消息错误，错误原因:{}", r.getMsg());
//                }
//            }, executor);
//        }

        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        MqLog annotation = method.getAnnotation(MqLog.class);

    }

}
